#include "ChatServer.hpp"
#include "ThreadPool.hpp"
#include "Route.hpp"
#include <memory>

void Usage(std::string proc)
{
    std::cerr << "Usage :" << proc << " loaclport" << std::endl;
}

using task_t = std::function<void()>;

// ./udp_server serverport
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    EnableConsoleLogStrategy();

    uint16_t localport = std::stoi(argv[1]);

    // 1.消息转发功能
    std::unique_ptr<Route> r = std::make_unique<Route>();

    // 2.线程池对象
    auto tp = ThreadPool<task_t>::GetInstance();

    // 3.服务器对象
    std::unique_ptr<ChatServer> ptr = std::make_unique<ChatServer>(localport,
                                                                   [&r, &tp](int sockfd, std::string message, InetAddr addr)
                                                                   {
                                                                       task_t task = std::bind(&Route::RouteMessageToAll, r.get(), sockfd, message, addr);
                                                                       tp->Enqueue(task);
                                                                   });
    ptr->Init();
    ptr->Start();

    return 0;
}